﻿namespace NJQuery {
	using System;
	using System.ComponentModel;
	using System.Web.UI;
	using System.Diagnostics.CodeAnalysis;

	/// <summary>
	/// Server control used to define the properties of a <see href="http://docs.jquery.com/UI/Effects/Size">jQuery UI Size effect</see>, which resizes an element to a specified width and height.
	/// </summary>
	public class SizeEffect : EffectBase {


		#region Constants: Property defaults

		private const string defaultOriginString = "Middle,Center";
		private const ScaleType defaultScale = ScaleType.Both;

		#endregion


		#region Static Readonly Fields: Property defaults

		private static readonly EffectOrigin defaultOrigin = EffectOrigin.MiddleCenter;

		#endregion


		#region Fields

		private EffectSize from;
		private EffectSize to;

		#endregion


		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="SizeEffect"/> class.
		/// </summary>
		public SizeEffect() : base("size") { }

		#endregion


		#region Properties: Initialization options

		/// <summary>
		/// The state at the beginning (usually not needed).
		/// This property maps directly to the "from" option when the jQuery UI effect is initialized.
		/// </summary>
		[DefaultValue(null), MergableProperty(false), PersistenceMode(PersistenceMode.InnerProperty), Category("Behavior")]
		public virtual EffectSize From {
			get {
				if(from == null) from = new EffectSize(ViewState);
				return from;
			}
		}

		/// <summary>
		/// Height and width to resize to.
		/// This property maps directly to the "to" option when the jQuery UI effect is initialized.
		/// </summary>
		[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "To", Justification = "Using jQuery UI naming, for better or worse.")]
		public virtual EffectSize To {
			get {
				if(to == null) to = new EffectSize(ViewState);
				return to;
			}
		}

		/// <summary>
		/// The vanishing point, default for show/hide.
		/// This property maps directly to the "origin" option when the jQuery UI effect is initialized.
		/// </summary>
		[DefaultValue(typeof(EffectOrigin), defaultOriginString), Category("Behavior")]
		public virtual EffectOrigin Origin {
			get { return ViewState["Origin"] as EffectOrigin? ?? defaultOrigin; }
			set { ViewState["Origin"] = value; }
		}

		/// <summary>
		/// Which areas of the element will be resized.
		/// This property maps directly to the "scale" option when the jQuery UI effect is initialized.
		/// </summary>
		public virtual ScaleType Scale {
			get { return ViewState["Scale"] as ScaleType? ?? defaultScale; }
			set { ViewState["Scale"] = value; }
		}

		#endregion


		#region Methods: Rendering hooks

		/// <summary>
		/// Allows a jQuery server control to reference scripts and stylesheets necessary to render their client markup and widgets.
		/// </summary>
		/// <param name="manager">The <see cref="Manager"/> used to handle style and script referencing in addition to unified generation of load and ready scripts.</param>
		protected override void Register(Manager manager) {
			base.Register(manager);

			manager.RegisterClientScript(ClientScript.UIScaleEffect); //Uses Scale script file
		}

		/// <summary>
		/// Returns a <see cref="PropertyCollection"/> containing option values specific to the effect.
		/// This method is used primarily by control developers.
		/// </summary>
		/// <returns>A <see cref="PropertyCollection"/> containing the effect's option values.</returns>
		protected override PropertyCollection CreateOptionProperties() {
			PropertyCollection properties = base.CreateOptionProperties();

			if(from != null && !from.IsDefault) properties.AddValue("from", from);

			if(to != null & !to.IsDefault) properties.AddValue("to", to);

			EffectOrigin origin = Origin;
			if(origin != defaultOrigin) properties.AddValue("origin", origin);

			ScaleType scale = Scale;
			if(scale != defaultScale) properties.AddEnumValue("scale", scale);

			return properties;
		}

		#endregion


	}

}